package yj.scms.goods.service.impl;


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import tk.mybatis.mapper.entity.Example;
import yj.scms.goods.mapper.ProductMapper;
import yj.scms.goods.mapper.ProductMapperXml;
import yj.scms.goods.service.ProductService;
import yj.scms.goodsapi.pojo.ProductEntity;

import java.util.List;
import java.util.Map;

@Service
public class ProductServiceImpl implements ProductService {

	//User通用Mapper查询数据
	@Autowired
	private ProductMapper productMapper;
	
	//Mybatis的XML查询数据
	@Autowired
	private ProductMapperXml productMapperXml;
	
	@Override
	public List<ProductEntity> pagehelperProductList(ProductEntity bean, Integer pageNo, Integer pageSize) {
		PageHelper.startPage(pageNo, pageSize);
		List<ProductEntity> select = productMapper.select(bean);
		return select;
	}
	
	@Override
	public List<Map<Object,Object>>  productXmlHash(){
		
		return productMapperXml.queryProductMap();
	}

	@Override
	public List<ProductEntity> productXmlBean() {
		List<ProductEntity> queryTestPojo = productMapperXml.queryProductPojo();
		return queryTestPojo;
	}

	@Override
	public PageInfo<ProductEntity> findAll(ProductEntity bean, Integer pageNo, Integer pageSize) {
		PageHelper.startPage(pageNo, pageSize);
		Example example = new Example(ProductEntity.class);
        if (!StringUtils.isEmpty(bean.getName())) {
            example.createCriteria().andEqualTo("name", bean.getName());
        }

        List<ProductEntity> users = productMapper.selectByExample(example);
        return new PageInfo<>(users);
	}

	@Override
	public Integer addProduct(ProductEntity bean) {
		Integer a = productMapper.insert(bean);
		return a;
	}

	@Override
	public ProductEntity queryProductById(Long id){
		Example example = new Example(ProductEntity.class);
        if (id!=null) {
            example.createCriteria().andEqualTo("id", id);
        }
        /*if (!StringUtils.isEmpty(user.getMobile())) {
            example.createCriteria().andEqualTo("mobile", user.getMobile());
        }*/
		ProductEntity bean = new ProductEntity();
		bean.setId(id);
		ProductEntity suo = productMapper.selectOne(bean);
		return suo;
	}

}
